package com.nan.gbd.library.sip.account;

import android.os.Build;

import com.nan.gbd.library.utils.AccountConfig;

import org.json.JSONObject;

public class DevConfig {
    public static final double INVALID_LONGITUDE = -99999;
    public static final double INVALID_LATITUDE = -99999;
    public static final int EXPIRED_VIDEO_DAY = 7;
    public static final int FREE_STORAGE_SPACE = 50;
    public static final int MAX_DURATION = 30;

    private int gmLevel;       // 国密35114等级

    private String name;           // 设备名称
    private String manufacturer;   // 设备生产商
    private String model;          // 设备型号
    private String firmware;       // 设备固件版本
    private String serialNumber;   // 设备序列号
    private int channel;           // 视频输入通道数
    private String recordPath;     // 视频录制存放地址

    private int port;              // 设备端口
    private String protocol;     // 信令传输协议

    private int regExpiry;         // 注册有效期(秒), 范围3600-100000
    private int heartbeatInterval; // 心跳周期(秒), 范围5-255
    private int heartbeatCount;    // 最大心跳超时次数, 范围3-255


    private String charset;       // 字符编码类型 GB2312 / UTF-8
    private Double longitude;      // 经度, 可自定义
    private Double latitude;       // 纬度, 可自定义

    private int inviteCount;       // 最大支持并发取流数
    private int voiceCount;        // 最大支持并发语音对讲数
    private int posCapability;     // 定位功能支持情况, 取值:0-不支持;1-支持GPS定位;2-支持北斗定位(可选, 默认取值为0)
    private boolean globalAuth;    // 是否使用全局注册认证信息

    private int expiredVideoDay;       // 本地视频存储最大天数
    private int freeStorageSpace;       // 本地存储空间最少空余(xxM)
    private int maxDuration;       // 视频录制最大时长

    public DevConfig(JSONObject json) {
        update(json);
        updateLocal();
    }

    public void update(JSONObject configJSON) {
        if (configJSON == null) {
            return;
        }

        if (configJSON.has("heartbeatInterval")) {
            setHeartbeatInterval(configJSON.optInt("heartbeatInterval"));
        }
        if (configJSON.has("heartbeatCount")) {
            setHeartbeatCount(configJSON.optInt("heartbeatCount"));
        }
        if (configJSON.has("protocol")) {
            setProtocol(configJSON.optString("protocol"));
        }
        if (configJSON.has("regExpiry")) {
            setRegExpiry(configJSON.optInt("regExpiry"));
        }
        if (configJSON.has("inviteCount")) {
            setInviteCount(configJSON.optInt("inviteCount"));
        }
        if (configJSON.has("voiceCount")) {
            setVoiceCount(configJSON.optInt("voiceCount"));
        }
        if (configJSON.has("charset")) {
            setCharset(configJSON.optString("charset"));
        }
        if (configJSON.has("channel")) {
            setChannel(configJSON.optInt("channel"));
        }
        if (configJSON.has("expiredVideoDay")) {
            setExpiredVideoDay(configJSON.optInt("expiredVideoDay"));
        }
        if (configJSON.has("freeStorageSpace")) {
            setFreeStorageSpace(configJSON.optInt("freeStorageSpace"));
        }
        if (configJSON.has("maxDuration")) {
            setMaxDuration(configJSON.optInt("maxDuration"));
        }
    }

    public void updateLocal() {
        setGmLevel(0);
        setName("Chaoying Sh-v6-xj");
        setManufacturer("ChaoYing");
        setModel("ChaoYing");
        setFirmware("ChaoYing");
        setSerialNumber("ChaoYing");
//        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.O) {
//            setSerialNumber(Build.SERIAL);
//        } else {
//            setSerialNumber(Build.getSerial());
//        }
        setPort(AccountConfig.initLocalPort());
        setRecordPath(AccountConfig.getRecordPath());
        setLongitude(INVALID_LONGITUDE);
        setLatitude(INVALID_LATITUDE);
    }


    public int getGmLevel() {
        return gmLevel;
    }

    public void setGmLevel(int gmLevel) {
        this.gmLevel = gmLevel;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getManufacturer() {
        return manufacturer;
    }

    public void setManufacturer(String manufacturer) {
        this.manufacturer = manufacturer;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public String getFirmware() {
        return firmware;
    }

    public void setFirmware(String firmware) {
        this.firmware = firmware;
    }

    public String getSerialNumber() {
        return serialNumber;
    }

    public void setSerialNumber(String serialNumber) {
        this.serialNumber = serialNumber;
    }

    public int getChannel() {
        return channel;
    }

    public void setChannel(int channel) {
        this.channel = channel;
    }

    public String getRecordPath() {
        return recordPath;
    }

    public void setRecordPath(String recordPath) {
        this.recordPath = recordPath;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getProtocol() {
        return protocol;
    }

    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }

    public int getRegExpiry() {
        return regExpiry;
    }

    public void setRegExpiry(int regExpiry) {
        this.regExpiry = regExpiry;
    }

    public int getHeartbeatInterval() {
        return heartbeatInterval;
    }

    public void setHeartbeatInterval(int heartbeatInterval) {
        this.heartbeatInterval = heartbeatInterval;
    }

    public int getHeartbeatCount() {
        return heartbeatCount;
    }

    public void setHeartbeatCount(int heartbeatCount) {
        this.heartbeatCount = heartbeatCount;
    }


    public String getCharset() {
        return charset;
    }

    public void setCharset(String charset) {
        this.charset = charset;
    }

    public Double getLongitude() {
        return longitude;
    }

    public void setLongitude(Double longitude) {
        this.longitude = longitude;
    }

    public Double getLatitude() {
        return latitude;
    }

    public void setLatitude(Double latitude) {
        this.latitude = latitude;
    }

    public int getInviteCount() {
        return inviteCount;
    }

    public void setInviteCount(int inviteCount) {
        this.inviteCount = inviteCount;
    }

    public int getVoiceCount() {
        return voiceCount;
    }

    public void setVoiceCount(int voiceCount) {
        this.voiceCount = voiceCount;
    }

    public int getPosCapability() {
        return posCapability;
    }

    public void setPosCapability(int posCapability) {
        this.posCapability = posCapability;
    }

    public boolean isGlobalAuth() {
        return globalAuth;
    }

    public void setGlobalAuth(boolean globalAuth) {
        this.globalAuth = globalAuth;
    }

    public int getExpiredVideoDay() {
        if (expiredVideoDay == 0) {
            expiredVideoDay = EXPIRED_VIDEO_DAY;
        }
        return expiredVideoDay;
    }

    public void setExpiredVideoDay(int expiredVideoDay) {
        this.expiredVideoDay = expiredVideoDay;
    }

    public int getFreeStorageSpace() {
        if (freeStorageSpace == 0) {
            freeStorageSpace = FREE_STORAGE_SPACE;
        }
        return freeStorageSpace;
    }

    public void setFreeStorageSpace(int freeStorageSpace) {
        this.freeStorageSpace = freeStorageSpace;
    }

    public int getMaxDuration() {
        if (maxDuration == 0) {
            maxDuration = MAX_DURATION;
        }
        return maxDuration;
    }

    public void setMaxDuration(int maxDuration) {
        this.maxDuration = maxDuration;
    }

    @Override
    public String toString() {
        return "DevConfig{" +
                "gmLevel=" + gmLevel +
                ", name='" + name + '\'' +
                ", manufacturer='" + manufacturer + '\'' +
                ", model='" + model + '\'' +
                ", firmware='" + firmware + '\'' +
                ", serialNumber='" + serialNumber + '\'' +
                ", channel=" + channel +
                ", recordPath='" + recordPath + '\'' +
                ", port=" + port +
                ", protocol='" + protocol + '\'' +
                ", regExpiry=" + regExpiry +
                ", heartbeatInterval=" + heartbeatInterval +
                ", heartbeatCount=" + heartbeatCount +
                ", charset='" + charset + '\'' +
                ", longitude=" + longitude +
                ", latitude=" + latitude +
                ", inviteCount=" + inviteCount +
                ", voiceCount=" + voiceCount +
                ", posCapability=" + posCapability +
                ", globalAuth=" + globalAuth +
                ", expiredVideoDay=" + expiredVideoDay +
                ", freeStorageSpace=" + freeStorageSpace +
                ", maxDuration=" + maxDuration +
                '}';
    }
}
